package com.fyzk.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.user.xiangxi.JCOrderDetailActivity;
import com.fyzk.activity.user.xiangxi.NumLotteryOrderDetailActivity;
import com.fyzk.activity.user.xiangxi.OldFootballOrderDetailActivity;
import com.fyzk.bean.BetRecordResponse;
import com.fyzk.betcode.AppLotteryCodeListManager;
import com.fyzk.constants.ConstantsDJ;
import com.fyzk.dialog.SureDialog;
import com.fyzk.net.PublicMethod;
import com.fyzk.utils.IntentParamUtils;
import com.fyzk.utils.LotteryIDUtils;
import com.tyjc.lottery.R;

import java.util.List;

/**
 * Created by qinkang on 2018/7/18.
 */

public class GoucaiItemAdapter extends BaseAdapter {
    private List<BetRecordResponse.OrdersBean> infors;
    protected Context context;

    public GoucaiItemAdapter(Context context, List<BetRecordResponse.OrdersBean> infors) {
        this.context = context;
        this.infors = infors;
    }

    @Override
    public int getCount() {
        if (infors != null) {
            return infors.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return infors.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.user_goucai_item, null);
            holder.bg = (LinearLayout) convertView
                    .findViewById(R.id.user_exp_child_ll);
            holder.title = (TextView) convertView
                    .findViewById(R.id.user_exp_child_title);
            holder.time = (TextView) convertView
                    .findViewById(R.id.user_exp_child_time);
            holder.money = (TextView) convertView
                    .findViewById(R.id.user_exp_child_money);
            holder.prizeMoney = (TextView) convertView
                    .findViewById(R.id.chasebet_beted);
            holder.tv_day = (TextView) convertView
                    .findViewById(R.id.tv_day);
            holder.tv_month = (TextView) convertView
                    .findViewById(R.id.tv_month);
            holder.tv_year = (TextView) convertView
                    .findViewById(R.id.tv_year);

            holder.lineUp = convertView.findViewById(R.id.line_up);
            holder.lineDown = convertView.findViewById(R.id.line_down);
            holder.rat = convertView.findViewById(R.id.rat);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final BetRecordResponse.OrdersBean info;

        if (position == 0) {
            holder.lineUp.setVisibility(View.INVISIBLE);
            holder.rat.setVisibility(View.VISIBLE);
        } else {
            holder.lineUp.setVisibility(View.VISIBLE);
            holder.rat.setVisibility(View.GONE);
        }
        if (position == infors.size() - 1) {
            holder.lineDown.setVisibility(View.GONE);
        } else {
            holder.lineDown.setVisibility(View.VISIBLE);
        }
        info = infors.get(position);
        holder.title.setText(info.getLotteryTypeName());

        String receiveTime = info.getReceiveTime();
        try {
            String[] split = receiveTime.split(" ");
            String[] ymd = split[0].split("-");
            holder.tv_year.setText(ymd[0]);
            holder.tv_month.setText(setMonth(ymd[1]));
            holder.tv_day.setText(ymd[2]);
            holder.time.setText(split[1]);
        } catch (Exception e) {
            holder.tv_year.setText("");
            holder.tv_month.setText("");
            holder.tv_day.setText("");
            holder.time.setText("");
        }

        if (info.getOrderStatus() == 3) { // 3:已出票
            holder.prizeMoney.setText(info.getOrderResultStatusName());
        } else {
            holder.prizeMoney.setText(info.getOrderStatusName());
        }
        holder.money.setText("投注:" + PublicMethod.formatMoney(info.getAmount()) + "米");
        if (info.getTotalPrize() != 0) {
            holder.prizeMoney.setTextColor(Color.parseColor("#FD4A4A"));
            holder.prizeMoney.setText("中奖:" + PublicMethod.formatMoney(info.getTotalPrize() * 1.0) + "米");
        } else {
            if (info.getOrderResultStatusName().contains("未开奖") || info.getOrderStatusName().contains("未开奖")) {
                holder.prizeMoney.setTextColor(Color.parseColor("#5C96F9"));
            } else {
                holder.prizeMoney.setTextColor(Color.parseColor("#666666"));
            }
            if (info.getOrderStatus() == 3) { // 3:已出票
                holder.prizeMoney.setText(info.getOrderResultStatusName());
            } else {
                holder.prizeMoney.setText(info.getOrderStatusName());
            }
        }

        holder.bg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isToGenDan(info.getLotteryType())) {
                    String pid = info.getOrderid() + "";
                    toOrderDetail(pid, info.getLotteryType() + "");
                }
            }
        });
        return convertView;
    }

    private String setMonth(String month) {
        if (month.equals("01")) {
            return "一月";
        } else if (month.equals("02")) {
            return "二月";
        } else if (month.equals("03")) {
            return "三月";
        } else if (month.equals("04")) {
            return "四月";
        } else if (month.equals("05")) {
            return "五月";
        } else if (month.equals("06")) {
            return "六月";
        } else if (month.equals("07")) {
            return "七月";
        } else if (month.equals("08")) {
            return "八月";
        } else if (month.equals("09")) {
            return "九月";
        } else if (month.equals("10")) {
            return "十月";
        } else if (month.equals("11")) {
            return "十一月";
        } else if (month.equals("12")) {
            return "十二月";
        }
        return "";
    }

    /**
     * 订单详细页
     *
     * @param id
     * @param lotteryType
     */
    protected void toOrderDetail(final String id, String lotteryType) {
        Intent intent;
        if (lotteryType.contains("30") || lotteryType.contains("40") || lotteryType.contains("50")) {
            AppLotteryCodeListManager.betType = AppLotteryCodeListManager.FIRSTENTER;
            intent = new Intent(context, JCOrderDetailActivity.class);
        } else {
            intent = new Intent(context, NumLotteryOrderDetailActivity.class);
        }
        intent.putExtra(IntentParamUtils.BETDETAILID, id);
        context.startActivity(intent);
    }


    private boolean isToGenDan(int lotteryId) {
        switch (lotteryId) {
//            case LotteryIDUtils.DCBF_LOTTERY_ID_INT:
//            case LotteryIDUtils.DCBQC_LOTTERY_ID_INT:
//            case LotteryIDUtils.DCSPF_LOTTERY_ID_INT:
//            case LotteryIDUtils.DCSXDS_LOTTERY_ID_INT:
//            case LotteryIDUtils.DCZJQ_LOTTERY_ID_INT:
//                return false;
            default:
                return true;
        }
    }

    public void setInfors(List<BetRecordResponse.OrdersBean> infors) {
        this.infors = infors;
        this.notifyDataSetChanged();
    }

    class ViewHolder {
        LinearLayout bg;
        TextView title;
        TextView time;
        TextView state;
        TextView money;
        TextView prizeMoney;
        TextView tv_day;
        TextView tv_month;
        TextView tv_year;
        View lineUp;
        View lineDown;
        View rat;
    }

}
